//
//  MainTableViewController.swift
//  SinaWeiBo
//
//  Created by 有风 on 16/2/2.
//  Copyright © 2016年 YouFeng. All rights reserved.
//

import UIKit
import SVProgressHUD
import AFNetworking

class HomeTableViewController: BaseTableViewContorller {

    private lazy var status = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        visitorLoginView?.setupInfoWith("关注一些人，回这里看看有什么惊喜", imageName: nil)
        if AccountViewModel.sharedAccountViewModel.access_token == nil {
            return
        }
        
        prepareForTableView()
        laodData()
    }

    private func prepareForTableView() {
        tableView.registerClass(HomeViewCell.self, forCellReuseIdentifier: "weibo")
        tableView.estimatedRowHeight = 300
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.separatorStyle = .None

        //系统下拉刷新控件
        refreshControl = UIRefreshControl()
        //添加触发事件
        refreshControl?.addTarget(self, action: "laodData", forControlEvents: .ValueChanged)
    
        //将菊花设置为tableFooterView
        tableView.tableFooterView = indicatorView
        
    }
    
    @objc private func laodData() {
        let urlsting = "https://api.weibo.com/2/statuses/home_timeline.json"
        guard let token = AccountViewModel.sharedAccountViewModel.access_token else{
            SVProgressHUD.showErrorWithStatus("请登录")
            return
        }
        
        let params = ["access_token" : token]
        
        let AFN = AFHTTPSessionManager()
        
        //加载以前的微博
        
        
        
        
        
        
        //转动小菊花
        refreshControl?.beginRefreshing()
        AFN.GET(urlsting, parameters: params, progress: nil, success: { (_, result) -> Void in
            //print(result)
            
            //停止动画
            self.refreshControl?.endRefreshing()
            
            
            guard let dict = result as? [String : AnyObject] else {
                SVProgressHUD.showErrorWithStatus("数据类型不匹配")
                return
            }
            
            guard let array = dict["statuses"] as? [[String : AnyObject]] else {
                SVProgressHUD.showErrorWithStatus("数据类型不匹配")
                return
            }
            
            //给status赋值
            var tempArr = [Status]()
            
            for item in array {
                let s = Status(dict: item)
                tempArr.append(s)
            }
            self.status = tempArr
            //print(self.status)
            self.tableView.reloadData()

            }) { (_, error) -> Void in
                print(error)
                
        }
    }
    
    //懒加载 菊花
    private lazy var indicatorView: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
    
    
        // MARK: - Table view data source

    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.status.count
    }

    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> HomeViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("weibo", forIndexPath: indexPath) as! HomeViewCell

        let statu:Status = self.status[indexPath.row]
        cell.status = statu
        cell.selectionStyle = .None
        
        if indexPath.row == self.status.count - 1 {
            indicatorView.startAnimating()
            
        }

        return cell
    }



}
